using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UIDesigner : MonoBehaviour
{
    public Ship m_Ship;
    public UIItem m_UIItemPrefab;
    public GameObject m_ShipUIListParent;
    public GameObject m_UIBackground;
    public List<UIItem> m_ShipUIItemList = new List<UIItem>();

    public List<ShipUIPreviewer> m_UIPreviewers = new List<ShipUIPreviewer>();

    public ShipUIPreviewer m_ShipUIPreviewerPrefab;
    public CommonParameterPanel m_CommonParameterPanel;
    public ShipUIParameterPanel m_SelectedParameterPanel;
    public List<ShipUIParameterPanel> m_ParameterPanels = new List<ShipUIParameterPanel>();
    private void Awake()
    {
    }

    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void ResizeBackground()
    {
        var rectTransform = m_UIBackground.GetComponent<RectTransform>();
        rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
        rectTransform.localScale = new Vector2(m_UIBackground.GetComponent<UIDesignBackground>().sizeScale, m_UIBackground.GetComponent<UIDesignBackground>().sizeScale);
        rectTransform.position = new Vector2(Screen.width / 2 - Screen.width * m_UIBackground.GetComponent<UIDesignBackground>().sizeScale / 2,
            Screen.height / 2 - Screen.height * m_UIBackground.GetComponent<UIDesignBackground>().sizeScale / 2);
    }
    public void MoveShipUIToShipFromUIDesigner()
    {
        if (m_Ship == null)
            return;
        m_Ship.m_ShipUIDatas.Clear();
        foreach (var i in m_UIPreviewers)
        {
            m_Ship.m_ShipUIDatas.Add(i.shipUI.shipUIData);
        }

    }
    public void MoveShipUIToUIDesignerFromShip()
    {
        if (m_Ship.m_ShipUIs.Count == 0)
            m_Ship.GenerateShipUIs();
        if (m_Ship == null)
            return;
        foreach ( var i in m_UIPreviewers)
        {
            Destroy(i.gameObject);
        }
        m_UIPreviewers.Clear();

        foreach (var i in m_Ship.m_ShipUIs)
        {
            var newPreviewer = CreatePreviewer(i);
            i.transform.SetParent(newPreviewer.transform);
            i.GetComponent<RectTransform>().sizeDelta = new Vector3(i.sizeScale.x * Screen.width, i.sizeScale.y * Screen.height);
            i.transform.localPosition = new Vector3(0,0,0);
            i.SetEditMode(true);
            newPreviewer.transform.localPosition = new Vector3(i.positionScale.x * Screen.width, i.positionScale.y * Screen.height);
            newPreviewer.GetComponent<RectTransform>().sizeDelta = new Vector3(i.sizeScale.x * Screen.width, i.sizeScale.y * Screen.height);
            newPreviewer.transform.localScale = new Vector3(1, 1, 1);
            m_UIPreviewers.Add(newPreviewer);
        }
        m_Ship.m_ShipUIs.Clear();
    }
    public void Init(Ship ship)
    {
        m_Ship = ship;
        ResizeBackground();
        MoveShipUIToUIDesignerFromShip();

    }
    public void SetPreviewersEditMode(bool isEditMode)
    {
        foreach (var i in m_UIPreviewers)
        {
            i.m_IsEditMode = isEditMode;
        }
    }

    public void OnBasicClicked()
    {
        FreshShipUIList();
    }

    void FreshShipUIList()
    {
        ClearShipUIItemList();

        foreach(var i in ShipUI.shipUIPrefabs)
        {
            var uiItem = Instantiate(m_UIItemPrefab);
            uiItem.Init(i, this);
            uiItem.transform.SetParent(m_ShipUIListParent.transform);
            uiItem.transform.SetSiblingIndex(m_ShipUIListParent.transform.childCount-2);
            m_ShipUIItemList.Add(uiItem);
        }
    }

    void ClearShipUIItemList()
    {
        foreach(var i in m_ShipUIItemList)
        {
            Destroy(i.gameObject);
        }
        m_ShipUIItemList.Clear();
    }

    public void OnBackgroundDrop(BaseEventData eventData)
    {
        if (!eventData.selectedObject)
            return;
        var previewer = eventData.selectedObject.GetComponent<ShipUIPreviewer>();
        if (!previewer)
            return;
        if (m_UIPreviewers.Contains(previewer))
            return;
        previewer.SetID(m_Ship.GetUINumber());
        m_UIPreviewers.Add(previewer);
        SetPreviewersRaycastTarget(true);
        eventData.selectedObject = null;
        previewer.shipUI.positionScale = new Vector2(previewer.transform.localPosition.x / (float)Screen.width, previewer.transform.localPosition.y / (float)Screen.height);
        previewer.shipUI.sizeScale = new Vector2(previewer.shipUI.GetComponent<RectTransform>().rect.width / (float)Screen.width, previewer.shipUI.GetComponent<RectTransform>().rect.height / (float)Screen.height);
        Debug.Log("pos="+ previewer.shipUI.positionScale+", size="+ previewer.shipUI.sizeScale);
    }
    public void OnBackgroundEnter(BaseEventData eventData)
    {
        if (!eventData.selectedObject || (eventData.selectedObject.GetComponent<ShipUIPreviewer>() == null))
            return;
        var rectTransform = eventData.selectedObject.GetComponent<RectTransform>();

        eventData.selectedObject.transform.SetParent(m_UIBackground.transform);
        eventData.selectedObject.transform.localScale = new Vector2(1, 1);
        eventData.selectedObject.transform.SetSiblingIndex(m_UIBackground.transform.childCount - 1);
    }
    public void OnBackgroundLeave(BaseEventData eventData)
    {
        if (!eventData.selectedObject || (eventData.selectedObject.GetComponent<ShipUIPreviewer>() == null))
            return;
        var rectTransform = eventData.selectedObject.GetComponent<RectTransform>();
    }
    public void SetPreviewersRaycastTarget(bool isEnabled)
    {
        foreach(var i in m_UIPreviewers)
        {
            i.SetRaycastTarget(isEnabled);
        }
    }
    public void OnShipUISelected(ShipUI shipUI)
    {
        var parameterPanel = FindParameterPanel(shipUI.m_Type);
        if (parameterPanel == null)
            return;
        Unselected();
        m_SelectedParameterPanel = parameterPanel;
        m_CommonParameterPanel.OnSelected(shipUI);
        m_SelectedParameterPanel.OnSelected(shipUI);
        
    }
    public ShipUIParameterPanel FindParameterPanel(ShipUI.ShipUIType type)
    {
        foreach(var i in m_ParameterPanels)
        {
            if(i.m_ParameterType == type)
            {
                return i;
            }
        }
        return null;
    }
    public void OnTrashCanDroped(BaseEventData eventData)
    {
        if (!eventData.selectedObject || (eventData.selectedObject.GetComponent<ShipUIPreviewer>() == null))
            return;
        var previwer = eventData.selectedObject.GetComponent<ShipUIPreviewer>();
        Destroy(previwer.gameObject);
        Unselected();
    }
    void Unselected()
    {
        m_CommonParameterPanel.Hide();
        if(m_SelectedParameterPanel != null)
            m_SelectedParameterPanel.Hide();
    }
    public ShipUIPreviewer CreatePreviewer(ShipUI shipUI)
    {
        var uiPreviewer = Instantiate(m_ShipUIPreviewerPrefab);
        uiPreviewer.GetComponent<RectTransform>().sizeDelta = shipUI.GetComponent<RectTransform>().sizeDelta;
        var canvas = Utils.GetCanvas();
        uiPreviewer.transform.SetParent(m_UIBackground.transform);
        uiPreviewer.transform.SetSiblingIndex(m_UIBackground.transform.childCount - 1);
        uiPreviewer.SetRaycastTarget(false);
        uiPreviewer.Init(shipUI, this);
        return uiPreviewer;
    }
}
